<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

// http://maestric.com/doc/php/codeigniter_models

class MY_Model extends CI_Model {

    var $table = "";

    function __construct() {
        parent::__construct();
        $this->load->database();
        //$this->system_log = My_Logs::instance(My_Logs::SERVER_LOG);
    }

    function insert($data, $return = true) {
        $data['register_date'] = $data['update_date'] = date($this->config->item('log_date_format'));
        $data['register_id'] = $this->session->userdata('user_id');
        $this->db->insert($this->table, $data);
        //$this->system_log->log('SQL QUERY: ' . $this->db->last_query());
        if ($return && $this->db->affected_rows() > 0)
            return $this->db->insert_id();
        return NULL;
    }

    function insert_batch($data) {
        $this->db->insert_batch($this->table, $data);
        //$this->system_log->log('SQL QUERY: ' . $this->db->last_query());
    }

    function find_id($id) {
        if ($id == NULL) {
            return NULL;
        }
        $this->db->where('id', $id);
        $query = $this->db->get($this->table);

        $result = $query->result();
        return count($result) > 0 ? $result[0] : NULL;
    }

    function find_row_where($where) {
        foreach ($where as $key => $value) {
            $this->db->where($key, $value);
        }
        $query = $this->db->get($this->table);
        if ($query) {
            $result = $query->row();
            return $result;
        }
        return null;
    }

    function find_all($sort = 'id', $order = 'asc') {
        $this->db->order_by($sort, $order);
        $this->db->order_by('update_date', 'desc');
        $query = $this->db->get($this->table);
        return $query->result();
    }

    function find_all_sort($sorts = array('id' => 'asc')) {
        foreach ($sorts as $key => $value) {
            $this->db->order_by($key, $value);
        }
        $query = $this->db->get($this->table);
        return $query->result();
    }

    function find_all_where($where, $sort = 'id', $order = 'asc') {
        foreach ($where as $key => $value) {
            $this->db->where($key, $value);
        }
        $this->db->order_by($sort, $order);
        $this->db->order_by('update_date', 'desc');
        $query = $this->db->get($this->table);
        return $query->result();
    }

    function update($id, $data) {
        $data['update_date'] = date($this->config->item('log_date_format'));
        $data['update_id'] = $this->session->userdata('user_id');
        $this->db->where('id', $id);
        $this->db->update($this->table, $data);
        return ( $this->db->affected_rows() > 0 ) ? TRUE : FALSE;
    }

    function delete($id) {
        if ($id != NULL) {
            $this->db->where('id', $id);
            $this->db->delete($this->table);

            return $this->db->affected_rows();
        }
    }

    function delete_where($where) {
        if (count($where) > 0) {
            foreach ($where as $key => $value) {
                $this->db->where($key, $value);
            }
            $this->db->delete($this->table);
            return $this->db->affected_rows();
        }
    }

    function delete_batch($field, $values) {
        $this->db->where_in($field, $values);
        $this->db->delete($this->table);
        return $this->db->affected_rows();
    }

}

/* End of file MY_Model.php */
/* Location: ./system/application/libraries/MY_Model.php */
